package org.iteam.xlz.core.service.membershipcard;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.membershipcard.LogMemberShipAccount;

public class CardAccountService extends BaseService<LogMemberShipAccount> {
	static private CardAccountService instance;
	
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;	
	private CardAccountService(){}
	
	public static CardAccountService getInstance(){
		if(null==instance){
			instance=new CardAccountService();
		}
		return instance;
	}
	
	public long counts(String mid,String logtype){
		try {
			params.clear();
			sql=new StringBuilder("select count(l.mid) from LogMemberShipAccount l left join membershipcard r on l.mid=r.mid left join userinfo u on r.uid=u.uid where 1=1");
			if(StringUtils.isNotBlank(mid)){
				sql.append(" and u.username like ?");
				params.add("%"+mid+"%");
			}
			if(StringUtils.isNotBlank(logtype)){
				sql.append(" and l.logtype=?");
				params.add(logtype);
			}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<LogMemberShipAccount> query(String mid,String logtype,long page,long rows){
		try {
			sql=new StringBuilder("select u.username as mid,l.amount,l.items,l.logtype,l.createtime from LogMemberShipAccount l left join membershipcard r on l.mid=r.mid left join userinfo u on r.uid=u.uid where 1=1");
			params.clear();
			if(StringUtils.isNotBlank(mid)){
				sql.append(" and u.username like ?");
				params.add("%"+mid+"%");
			}
			if(StringUtils.isNotBlank(logtype)){
				sql.append(" and l.logtype=?");
				params.add(logtype);
			}
			sql.append(" limit ?,?");
			params.add((page-1)*rows);
			params.add(rows); 
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<LogMemberShipAccount>();
		}
	}
}
